<?php

namespace app\admin\controller;

use think\Controller;

use think\Request;

use think\Session;

use app\common\model\Login as AdminModel;

class Login extends Controller

{

    public function login()

    {

        return $this->fetch();

    }

    public function checklogin(request $request){

    	$name = input("name");
    	$pass = md5(input("pwd"));
    	$user = AdminModel::where("ma_n",$name)->where("ma_p",$pass)->find();
    	//登录成功产生随机码
        if($user){  
            //将登录记录存入系统中
            $data['ma_id']  =  $user->id;
            $data['ma_name']  = $user->ma_n; 
            $data['ma_addtime']  = time(); 
            $request = Request::instance();
            $data['ma_ip']  = $request->ip(); 
            $data['ma_system']  = PHP_OS;
            $saveLog = db("guanli_log")->insert($data);
            if($saveLog){
                Session::set('UserName',$user->ma_n);
                Session::set('UserLever',$user->ma_l);
                Session::set('UserId',$user->id);   
                return 1;
            }else{
                 return 0;   
            }

        }else{

            //return $this->error('账号或者密码错误!');

            return 2;

        }

    }



    public function out(){

       Session::delete('UserName');

       Session::delete('UserId');

       return $this->redirect("__ROOT__/admin/login/login");

    }

}

